import { getOrderList,comfirmOrder, toCancelOrder } from '../../../../../common/api/index'
Page({
    data: {
		tabList:[
			{name:'全部',img:'/static/image/order.png',status: null,index:0 },
			{name:'未付款',img:'/static/image/order.png',status:'1',index:1 },
			{name:'待发货',img:'/static/image/order.png',status:'2,11',index:2 },
			{name:'已发货',img:'/static/image/order.png',status:'3',index:3 },
			{name:'已收货',img:'/static/image/order.png',status:'4',index:4  },
			{name:'售后',img:'/static/image/order.png',status:'5,6',index:5  },
		],
		showLoading: false,
		dataList:[],
		total: 0,
		activeCurrent: 0,
		pageNo: 1,
		status: null,
    },
    onLoad(options) {
		// 我的 进入
		if(options.name && options.status ){
			this.setData({
				status: options.status
			})
			this.getGoodsOrderList();
			// 高亮显示那个列表
			this.data.tabList.forEach((item,index) => {
				if(item.name == options.name){
					this.setData({
						activeCurrent: index
					})
				}	
			})
		}
		// 从支付成功页面进来
		if(options.fromType == 'paySuccess'){	
			this.setData({
				activeCurrent: 0
			})		
			this.getGoodsOrderList();
		}
    },
    onShow() {

	},
	// 取消订单
	toCancelOrder(e){
		let that = this
		let data = e.currentTarget.dataset.item;
		wx.showModal({
			title: '提示',
			content: '是否确认取消订单',
			success (res) {
			  if (res.confirm) {
					toCancelOrder(data.orderId).then(res => {
						if(res.code == 200){
							wx.showToast({
								title: '取消订单成功！',
								icon: 'success',
								duration: 2000
							})
							that.setData({
								pageNo: 1
							})
							that.getGoodsOrderList()
						}
					})
			  }
			}
		})
	},
	// 切换tab
	onChange(e){
		let name = e.detail.title
		this.data.tabList.forEach((item,index) => {
			if(item.name == name){
				this.setData({
					activeCurrent: item.index,
					status: item.status,
					pageNo: 1
				})
			}	
		})
		this.getGoodsOrderList()
	},
	// 获取订单列表
	getGoodsOrderList(){
		let data = {
			pageNo: this.data.pageNo,
			pageSize: 10
		}
		if(this.data.status){
			data['status'] = this.data.status
		}
		getOrderList(data).then(res => {
			if(res.code == 200){
				res.data.forEach(item => {
					if(item.orderStatus == 1){
						item.statusName = '未付款'
					}
					if(item.orderStatus == 2 || item.orderStatus == 11){
						item.statusName = '待发货'
					}
					if(item.orderStatus == 3){
						item.statusName = '已发货'
					}
					if(item.orderStatus == 4){
						item.statusName = '已收货'
					}
					if(item.orderStatus == 5){
						item.statusName = '已取消'
					}
					if(item.orderStatus == 6){
						item.statusName = '已退款'
					}
				})
				let data = []
				if(this.data.pageNo == 1){
					data = res.data
				}else{
					data = this.data.dataList.concat(res.data)
				}
				this.setData({
					dataList: data,
					total: res.extend.total,
					showLoading: false
				})
			}
		})
	},
	// 触底加载更多
    onReachBottom() {
		if(this.data.total > this.data.dataList.length){
			this.setData({
				pageNo: ++this.data.pageNo,
			})
			this.getGoodsOrderList()
		}
    },
	// 如果是支付成功进来的，则需要返回到tabbar页面 我的
    onUnload() {
		let pages = getCurrentPages();
		let currPage = null;
		// console.log(pages) 的到一个数组
		if (pages.length) {
			// 获取当前页面的对象（上边所获得的数组中最后一项就是当前页面的对象）
			currPage = pages[pages.length - 2];
		}
		if(currPage && currPage.route == 'pages/component/classify/paySuccess/index'){
			wx.switchTab({
				url: '/pages/tabbar/mine/index/index',
			})
		}
	},
	// 点击商品进入详情
	clickToPath(e){
		let path = e.currentTarget.dataset.path;
		wx.$goTo(path,true)
	},
	// 未支付订单  ----- 立即支付
	toPayOrder(e){
		let data = e.currentTarget.dataset.item;
		let ids = [];
		data.details.forEach(item => {
			ids.push(item.productId)
		})
		if(ids.length > 0){
			ids = ids.join(',')
			let path = '/pages/component/classify/pay/index?orderId='+data.orderId+'&productIds='+ids+"&totalMoney="+ data.payAmount
			wx.$goTo(path,true)
		}
	},
	// 确认收获
	toComfirmOrder(e){
		let that = this
		wx.showModal({
			title: '提示',
			content: '是否确认收货？',
			success (res) {
			  if (res.confirm) {
				let info = e.currentTarget.dataset.item;
				comfirmOrder(info.orderId).then(res => {
					if(res.code == 200){
						wx.showToast({
							title: '收货成功！',
							icon: 'success',
							duration: 2000
						})
						let data = that.data.dataList
						data.forEach(item => {
							if(item.orderId == info.orderId){
								item.statusName = '已收货'
								item.orderStatus = 4
							}
						})
						that.setData({
							pageNo: 1,
							dataList: data
						})
						that.getGoodsOrderList()
					}
				})
			  }
			}
		})
	},
})